package utils;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import servlet.UserServlet;


/** 
* @ClassName: SysConfig 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author undoner@gmail.com
* @date 2017年1月23日 下午2:14:41 
*/
public class SysConfig {
	private static final Logger logger = LoggerFactory.getLogger(SysConfig.class);
    public static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    public static String JDBC_URL = "jdbc:mysql://localhost:3306/demo";
    public static String JDBC_USR = "root";
    public static String JDBC_PWD = "root";
    public static Boolean IS_STORE_DB = false;

    static Properties pp = null;

    static {
        load();
    }

    public static void load() {
    	logger.debug("Start Loading System Config File.");

        pp = new Properties();
        try {
            InputStream is = SysConfig.class.getClassLoader().getResourceAsStream("install.properties");
            pp.load(is);
            is.close();

            JDBC_DRIVER = getStringProperty("jdbc.driver", JDBC_DRIVER);
            JDBC_URL = getStringProperty("jdbc.url", JDBC_URL);
            JDBC_USR = getStringProperty("jdbc.usr", JDBC_USR);
            JDBC_PWD = getStringProperty("jdbc.pwd", JDBC_PWD);
            IS_STORE_DB = getBooleanProperty("is.store.db", IS_STORE_DB);
        
        } catch (IOException e) {
        	logger.error(e.getMessage());
        } catch (Exception e) {
        	logger.error("Load SysConfig Error: "+ e.getMessage());
        }

        logger.debug("System Config File Loaded.");

    }


    private static int getIntegerProperty(String key, int defaultValue) {
        if (pp == null || pp.getProperty(key) == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(pp.getProperty(key));
        } catch (Exception e) {
        	logger.error(e.getMessage());
        }
        return defaultValue;
    }

    private static long getLongProperty(String key, long defaultValue) {
        if (pp == null || pp.getProperty(key) == null) {
            return defaultValue;
        }
        try {
            return Long.parseLong(pp.getProperty(key));
        } catch (Exception e) {
        	logger.error(e.getMessage());
        }
        return defaultValue;
    }
    
    private static boolean getBooleanProperty(String key, boolean defaultValue) {
        if (pp == null || pp.getProperty(key) == null) {
            return defaultValue;
        }
        try {
            return Boolean.valueOf(pp.getProperty(key));
        } catch (Exception e) {
        	logger.error(e.getMessage());
        }
        return defaultValue;
    }

    private static String getStringProperty(String key, String defaultValue) {
        if (pp == null || pp.getProperty(key) == null) {
            return defaultValue;
        }
        return pp.getProperty(key).trim();
    }

    public static void main(String args[]) {
        load();
    }

}
